/*
 * StudentDetail.java
 *
 * This program demonstrate the use of table.
 *
 * Copyright (c) 2007 Aptech Software Limited. All Rights Reserved.
 */
package demo.controls;

import java.util.Vector;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Vincent
 */
public class StudentDetail extends javax.swing.JFrame {

    /**
     * Creates new form StudentDetail
     */
    public StudentDetail() {
        initComponents();

        // init table
        tblStudent.setColumnSelectionAllowed(false);
        tblStudent.setRowSelectionAllowed(true);
        //tblStudent.setCellSelectionEnabled(true);
        DefaultTableModel dtm = (DefaultTableModel) tblStudent.getModel();
        Vector row = new Vector();

        row.add(100);
        row.add("AAA");
        row.add("Male");
        row.add("HCM");

        dtm.addRow(row);

        row = new Vector();
        row.add(200);
        row.add("BBB");
        row.add("Female");
        row.add("Ha Noi");

        dtm.insertRow(0, row);

        tblStudent.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting() == false) {
                    int index = tblStudent.getSelectedRow();
                    String strName = (String) tblStudent.getValueAt(index, 1);
                    String strCity = (String) tblStudent.getValueAt(index, 3);
                    txtName.setText(strName + " belongs to " + strCity);
                    String strIndex = String.valueOf(index + 1);
                    txtRow.setText(strIndex);
                }
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblPersonal = new javax.swing.JLabel();
        scpDetails = new javax.swing.JScrollPane();
        tblStudent = new javax.swing.JTable();
        lblRow = new javax.swing.JLabel();
        txtRow = new javax.swing.JTextField();
        txtName = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Student's Detail");
        setResizable(false);
        getContentPane().setLayout(new java.awt.FlowLayout());

        lblPersonal.setText("Student's personal details");
        lblPersonal.setPreferredSize(new java.awt.Dimension(450, 20));
        getContentPane().add(lblPersonal);

        scpDetails.setPreferredSize(new java.awt.Dimension(450, 120));

        tblStudent.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Seriel no.", "Name", "Gender", "City"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblStudent.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        scpDetails.setViewportView(tblStudent);

        getContentPane().add(scpDetails);

        lblRow.setText("Row no.");
        lblRow.setPreferredSize(new java.awt.Dimension(50, 20));
        getContentPane().add(lblRow);

        txtRow.setEditable(false);
        txtRow.setPreferredSize(new java.awt.Dimension(40, 20));
        getContentPane().add(txtRow);

        txtName.setEditable(false);
        txtName.setPreferredSize(new java.awt.Dimension(350, 20));
        txtName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNameActionPerformed(evt);
            }
        });
        getContentPane().add(txtName);

        setSize(new java.awt.Dimension(519, 257));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNameActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                new StudentDetail().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblPersonal;
    private javax.swing.JLabel lblRow;
    private javax.swing.JScrollPane scpDetails;
    private javax.swing.JTable tblStudent;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtRow;
    // End of variables declaration//GEN-END:variables
}
